﻿@* Generator : WebPagesHelper *@

@using System.Diagnostics
@using System.Web.WebPages.Scope
@using System.Web.UI.WebControls
@using System.Globalization
@using Microsoft.Internal.Web.Utils

@functions {
    private const string DefaultWidth = "300px";
    private const string DefaultHeight = "300px";
    private static readonly object _mapIdKey = new object();
    private static readonly object _mapQuestApiKey = new object();
    private static readonly object _bingApiKey = new object();
    private static readonly object _yahooApiKey = new object();

    public static string MapQuestApiKey {
        get {
            return (string)ScopeStorage.CurrentScope[_mapQuestApiKey];
        }
        set {
            ScopeStorage.CurrentScope[_mapQuestApiKey] = value;
        }
    }

    public static string YahooApiKey {
        get {
            return (string)ScopeStorage.CurrentScope[_yahooApiKey];
        }
        set {
            ScopeStorage.CurrentScope[_yahooApiKey] = value;
        }
    }

    public static string BingApiKey {
        get {
            return (string)ScopeStorage.CurrentScope[_bingApiKey];
        }
        set {
            ScopeStorage.CurrentScope[_bingApiKey] = value;
        }
    }

    // allow for stubbing this static resource in tests
    private static Func<HttpContextBase> _getCurrentHttpContext = (Func<HttpContextBase>)(() => new HttpContextWrapper(HttpContext.Current));
    internal static Func<HttpContextBase> GetCurrentHttpContext {
        private get {
            return _getCurrentHttpContext;
        }
        set {
            _getCurrentHttpContext = value;
        }
    }

    private static int MapId {
        get {
            var value = (int?)(GetCurrentHttpContext().Items[_mapIdKey]);
            return value.GetValueOrDefault();
        }
        set {
            GetCurrentHttpContext().Items[_mapIdKey] = value;
        }
    }
    
    private static string GetMapElementId() {
        return "map_" + MapId;   
    }
    
    private static string TryParseUnit(string value, string defaultValue) {
        if (String.IsNullOrEmpty(value)) {
            return defaultValue;
        }
        try {
            return Unit.Parse(value, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
        } catch (ArgumentException) {
            return defaultValue;    
        }    
    }

    private static IHtmlString RawJS(string text) {
        return Raw(HttpUtility.JavaScriptStringEncode(text));
    }
    
    private static IHtmlString Raw(string text) {
        return new HtmlString(text);    
    }
    
    private static string GetApiKey(string apiKey, object scopeStorageKey) {
        if (apiKey.IsEmpty()) {
            return (string)ScopeStorage.CurrentScope[scopeStorageKey];   
        }
        return apiKey;   
    }

    public class MapLocation {
        private readonly string _latitude;
        private readonly string _longitude;
        public MapLocation(string latitude, string longitude) {
            _latitude = latitude;
            _longitude = longitude;
        }

        public string Latitude {
            get { return _latitude; }
        }

        public string Longitude {
            get { return _longitude; }
        }
    }

    internal static string GetDirectionsQuery(string location, string latitude, string longitude, Func<string, string> encoder = null) {
        encoder = encoder ?? HttpUtility.UrlEncode;
        Debug.Assert(!(location.IsEmpty() && latitude.IsEmpty() && longitude.IsEmpty()));
        if (location.IsEmpty()) {
            return encoder(latitude + "," + longitude);
        }
        return encoder(location);
    }
}

@**
Summary:
    Generates Html to display a Map Quest map.
Parameter:
    key: Map Quest API key
Parameter location:
    Address of the location to center the map at
Parameter latitude:
    Latitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter longitude:
    Longitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter width: 
    Width of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter height: 
    Height of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter zoom:
    Initial zoom level of the map. Defaults to 7.
Parameter type:
    Map type to display. Valid values are "map", "sat" or "hyb".
Parameter showDirectionsLink:
    Determines if a link to get directions should be displayed when a location is specified. Defaults to true.
Parameter directionsLinkText
    The text for the get directions link. Defaults to "Get Directions".
**@
@helper GetMapQuestHtml(string key = null, string location = null, string latitude = null, string longitude = null, string width = "300px", string height = "300px", int zoom = 7, string type = "map",
            bool showDirectionsLink = true, string directionsLinkText = "Get Directions", bool showZoomControl = true, IEnumerable<MapLocation> pushpins = null) {
    key = GetApiKey(key, _mapQuestApiKey);
    if (key.IsEmpty()) {
        throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "key");  
    }
    
    string mapElement = GetMapElementId();
    string loc = "null"; // We want to print the value 'null' in the client
    if (latitude != null && longitude != null) {
        loc = String.Format(CultureInfo.InvariantCulture, "{{lat: {0}, lng: {1}}}",
            HttpUtility.JavaScriptStringEncode(latitude, addDoubleQuotes: false), HttpUtility.JavaScriptStringEncode(longitude, addDoubleQuotes: false));
    }

    // The MapQuest key listed on their website is Url encoded to begin with. 
    <script src="http://mapquestapi.com/sdk/js/v6.0.0/mqa.toolkit.js?key=@key" type="text/javascript"></script>
    <script type="text/javascript">
        MQA.EventUtil.observe(window, 'load', function() {
            var map = new MQA.TileMap(document.getElementById('@mapElement'), @zoom, @Raw(loc), '@RawJS(type)'); 
            @if (showZoomControl) {
            <text>
                MQA.withModule('zoomcontrol3', function() {
	                map.addControl(new MQA.LargeZoomControl3(), new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT));
                });
            </text>
            }
            @if (!String.IsNullOrEmpty(location)) {
            <text>
                MQA.withModule('geocoder', function() {
                    map.geocodeAndAddLocations('@RawJS(location)');
                });
            </text>
            }
            @if (pushpins != null) {
                foreach (var p in pushpins) {
                    @: map.addShape(new MQA.Poi({lat:@RawJS(p.Latitude),lng:@RawJS(p.Longitude)}));
	            }
            }
        });
    </script>
    
    <div id="@mapElement" style="width:@TryParseUnit(width, DefaultWidth); height:@TryParseUnit(height, DefaultHeight);">
    </div>
    if (showDirectionsLink) {
        <a class="map-link" href="http://www.mapquest.com/?q=@GetDirectionsQuery(location, latitude, longitude)">@directionsLinkText</a>
    }
    MapId++;
}

@**
Summary:
    Generates Html to display Bing map.
Parameter:
    key: Bing Maps application key
Parameter location:
    Address of the location to center the map at
Parameter latitude:
    Latitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter longitude:
    Longitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter width: 
    Width of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter height: 
    Height of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter zoom:
    Initial zoom level of the map. Defaults to 14.
Parameter type:
    Map type to display. Valid values are "auto", "aerial", "birdeye", "road" and "mercator".
Parameter useAdaptiveOverlay:
    Determines if the map overlay that adapts to the colors of the current map view is used. Defaults to true.
Parameter showDirectionsLink:
    Determines if a link to get directions should be displayed when a location is specified. Defaults to true.
Parameter directionsLinkText
    The text for the get directions link. Defaults to "Get Directions".
**@
@helper GetBingHtml(string key = null, string location = null, string latitude = null, string longitude = null, string width = null, string height = null, int zoom = 14, string type = "auto",
            bool useAdaptiveOverlay = true, bool showDirectionsLink = true, string directionsLinkText = "Get Directions", IEnumerable<MapLocation> pushpins = null) {
    key = GetApiKey(key, _bingApiKey);
    if (key.IsEmpty()) {
        throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "key");
    } 
    string mapElement = GetMapElementId();
    
    type = (type ?? "auto").ToLowerInvariant();

    <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(window).load(function() { 
            var map = null;
            @if (useAdaptiveOverlay) {
                <text>
                Microsoft.Maps.loadModule('Microsoft.Maps.Overlays.Style', {
                    callback: function () {
                        map = new Microsoft.Maps.Map(document.getElementById("@mapElement"), { credentials: '@RawJS(key)', mapTypeId: Microsoft.Maps.MapTypeId['@RawJS(type)'], customizeOverlays: true });

                </text>
            } else {
                <text>
                map = new Microsoft.Maps.Map(document.getElementById("@mapElement"), { credentials: '@RawJS(key)', mapTypeId: Microsoft.Maps.MapTypeId['@RawJS(type)'] });
                </text>
            }
            @if (latitude != null && longitude != null) {
                @: map.setView({zoom: @zoom, center: new Microsoft.Maps.Location(@RawJS(latitude), @RawJS(longitude))});
            }
            else if (location != null) {
                <text>
                map.getCredentials(function(credentials) {
                    $.ajax({
                        url: 'http://dev.virtualearth.net/REST/v1/Locations/' + encodeURI('@RawJS(location)'),
                        type: 'GET',
                        crossDomain: true,
                        data: { output: 'json', key: credentials }, dataType: 'jsonp', jsonp: 'jsonp',
                        success: function(data) {
                            if (data && data.resourceSets && data.resourceSets.length > 0 && data.resourceSets[0].resources && data.resourceSets[0].resources.length > 0) {
                                var r = data.resourceSets[0].resources[0].point.coordinates;
                                var loc = new Microsoft.Maps.Location(r[0], r[1]);
                                map.setView({zoom: @zoom, center: loc});              
                                map.entities.push(new Microsoft.Maps.Pushpin(loc, null));
                            }
                        }
                    });
                });
                </text>
            }
            @if (pushpins != null) {
                foreach(var loc in pushpins) {
                    @: map.entities.push(new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(@RawJS(loc.Latitude), @RawJS(loc.Longitude)), null));
                }
            }
            @if (useAdaptiveOverlay) {
                <text>
                    }
                });
                </text>
            }
        });
    </script>
    
    <div class="map" id="@mapElement" style="position:relative; width:@TryParseUnit(width, DefaultWidth); height:@TryParseUnit(height, DefaultHeight);">
    </div>
    if (showDirectionsLink) {
        // Review: Need to figure out if the link needs to be localized. 
        <a class="map-link" href="http://www.bing.com/maps/?v=2&where1=@GetDirectionsQuery(location, latitude, longitude)">@directionsLinkText</a>
    }
    MapId++;
}

@**
Summary:
    Generates Html to display a Google map.
Parameter location:
    Address of the location to center the map at
Parameter latitude:
    Latitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter longitude:
    Longitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter width: 
    Width of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter height: 
    Height of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter zoom:
    Initial zoom level of the map. Defaults to 14.
Parameter type:
    Map type to display. Valid values are "ROADMAP", "HYBRID", "SATELLITE" and "TERRAIN".
Parameter showDirectionsLink:
    Determines if a link to get directions should be displayed when a location is specified. Defaults to true.
Parameter directionsLinkText
    The text for the get directions link. Defaults to "Get Directions".
**@
@helper GetGoogleHtml(string location = null, string latitude = null, string longitude = null, string width = null, string height = null, int zoom = 14, string type = "ROADMAP",
                bool showDirectionsLink = true, string directionsLinkText = "Get Directions", IEnumerable<MapLocation> pushpins = null) {
    string mapElement = GetMapElementId();
    type = (type ?? "ROADMAP").ToUpperInvariant(); // Map types are in upper case

    // Google maps does not support null centers. We'll set it to arbitrary values if they are null and only the location is provided.
    // These locations are somewhere around Microsoft's Redmond Campus.
    latitude = latitude ?? "47.652437";
    longitude = longitude ?? "-122.132424";

    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            var map = new google.maps.Map(document.getElementById("@mapElement"), { zoom: @zoom, center: new google.maps.LatLng(@RawJS(latitude), @RawJS(longitude)), mapTypeId: google.maps.MapTypeId['@RawJS(type)'] });
            @if (!String.IsNullOrEmpty(location)) {
                <text>
                new google.maps.Geocoder().geocode({address: '@RawJS(location)'}, function(response, status) {
                    if (status === google.maps.GeocoderStatus.OK) {
                        var best = response[0].geometry.location;
                        map.panTo(best);
                        new google.maps.Marker({map : map, position: best });
                    }
                });
                </text>
            }
            @if (pushpins != null) {
                foreach(var loc in pushpins) {
                    @: new google.maps.Marker({map : map, position: new google.maps.LatLng(@RawJS(loc.Latitude), @RawJS(loc.Longitude))});
                }
            }
        });
    </script>
    
    <div class="map" id="@mapElement" style="width:@TryParseUnit(width, DefaultWidth); height:@TryParseUnit(height, DefaultHeight);">
    </div>
    if (showDirectionsLink) {
        <a class="map-link" href="http://maps.google.com/maps?q=@GetDirectionsQuery(location, latitude, longitude)">@directionsLinkText</a>
    }
    MapId++;
}

@**
Summary:
    Generates Html to display a Yahoo map.
Parameter:
    key: Yahoo application ID
Parameter location:
    Address of the location to center the map at
Parameter latitude:
    Latitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter longitude:
    Longitude to center on. If both latitude and longitude are specified, location is ignored.
Parameter width: 
    Width of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter height: 
    Height of the map with units such as 480px, 100% etc. Defaults to 300px.
Parameter zoom:
    Initial zoom level of the map. Defaults to 4.
Parameter type:
    Map type to display. Valid values are "YAHOO_MAP_SAT", "YAHOO_MAP_HYB" and "YAHOO_MAP_REG". 
Parameter showDirectionsLink:
    Determines if a link to get directions should be displayed when a location is specified. Defaults to true.
Parameter directionsLinkText
    The text for the get directions link. Defaults to "Get Directions".
**@
@helper GetYahooHtml(string key = null, string location = null, string latitude = null, string longitude = null, string width = null, string height = null, int zoom = 4, string type = "YAHOO_MAP_REG",
                bool showDirectionsLink = true, string directionsLinkText = "Get Directions", IEnumerable<MapLocation> pushpins = null) {
    key = GetApiKey(key, _yahooApiKey);
    if (key.IsEmpty()) {
        throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "key");
    }                     
    string mapElement = GetMapElementId();

    <script src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=@HttpUtility.UrlEncode(key)" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            var map = new YMap(document.getElementById('@RawJS(mapElement)'));  
            map.addTypeControl();  
            map.setMapType(@RawJS(type));  
            @if (latitude != null && longitude != null) {
                @: map.drawZoomAndCenter(new YGeoPoint(@RawJS(latitude), @RawJS(longitude)), @zoom);
            }
            else if (!String.IsNullOrEmpty(location)) {
                @: map.drawZoomAndCenter('@RawJS(location)', @zoom);
            }
            else {
                @: map.setZoomLevel(@zoom);
            }
            @if(pushpins != null) {
                foreach (var loc in pushpins) {
                     @: map.addMarker(new YGeoPoint(@RawJS(loc.Latitude), @RawJS(loc.Longitude)));
                 }
            }

        });
    </script>
    
    <div id="@mapElement" style="width:@TryParseUnit(width, DefaultWidth); height:@TryParseUnit(height, DefaultHeight);">
    </div>
    if (showDirectionsLink) {
        <a class="map-link" href="http://maps.yahoo.com/#q1=@GetDirectionsQuery(location, latitude, longitude, HttpUtility.UrlPathEncode)">@directionsLinkText</a>
    }
    MapId++;
}

